<?php
defined('IN_IA') or exit('Access Denied');
require_once 'defines.php';
require_once MODULE_FUNC.'functions.php';
class Asterye_waimaixiaModuleSite extends WeModuleSite {
    public function refund1($refund_id) {
		load()->classs('pay');
		global $_W;
		$refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
		$params = array('uniontid' => $refundlog['uniontid']);
		$params['uniacid'] = $refundlog['is_wish'] == 1 ? $refundlog['uniacid'] : $_W['uniacid'];
		$paylog = pdo_get('core_paylog', $params);
		if ($paylog['type'] == 'wechat' || $paylog['type'] == 'wxapp') {
			$refund_param = $this->reufnd_wechat_build1($refund_id, $refundlog['is_wish']);
			if (is_error($refund_param)) {
				return $refund_param;
			}
			if ($refundlog['is_wish'] == 1) {
				$module = 'store';
				$cert_file = ATTACHMENT_ROOT . 'store_wechat_refund_all.pem';
			} else {
				$module = '';
				$cert_file = ATTACHMENT_ROOT . $_W['uniacid'] . '_wechat_refund_all.pem';
			}

			$wechat = Pay::create('wechat', $module);
			$response = $wechat->refund($refund_param, $module);
			unlink($cert_file);
			if (is_error($response)) {
				pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
				return $response;
			} else {
				return $response;
			}
		} elseif ($paylog['type'] == 'alipay') {
			$refund_param = reufnd_ali_build1($refund_id, $refundlog['is_wish']);
			if (is_error($refund_param)) {
				return $refund_param;
			}
			$module = $refundlog['is_wish'] == 1 ? 'store' : '';
			$ali = Pay::create('alipay', $module);
			$response = $ali->refund($refund_param, $refund_id);
			if (is_error($response)) {
				pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
				return $response;
			} else {
				return $response;
			}
		}
		return error(1, '此订单退款方式不存在');
	}

	public function reufnd_wechat_build1($refund_id, $is_wish = 0) {
		global $_W;
		if ($is_wish == 1) {
			$setting = setting_load('store_pay');
			$pay_setting = $setting['store_pay'];
			$refund_setting = $setting['store_pay']['wechat_refund'];
		} else {
			$setting = uni_setting_load('payment', $_W['uniacid']);
			$pay_setting = $setting['payment'];
			$refund_setting = $setting['payment']['wechat_refund'];
		}

		if ($refund_setting['switch'] != 1) {
			return error(1, '未开启微信退款功能！');
		}
		if (empty($refund_setting['key']) || empty($refund_setting['cert'])) {
			return error(1, '缺少微信证书！');
		}

		$refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
		$uniacid = $is_wish == 1 ? $refundlog['uniacid'] : $_W['uniacid'];
		$paylog = pdo_get('core_paylog', array('uniacid' => $uniacid, 'uniontid' => $refundlog['uniontid']));
		$account = uni_fetch($uniacid);
		$refund_param = array(
			'appid' => $is_wish == 1 ? $pay_setting['wechat']['appid'] : $account['key'],
			'mch_id' => $pay_setting['wechat']['mchid'],
			'out_trade_no' => $refundlog['uniontid'],
			'out_refund_no' => $refundlog['refund_uniontid'],
			'total_fee' => $paylog['card_fee'] * 100,
			'refund_fee' => $refundlog['fee'] * 100,
			'nonce_str' => random(8),
			'refund_desc' => $refundlog['reason']
		);

		$cert = authcode($refund_setting['cert'], 'DECODE');
		$key = authcode($refund_setting['key'], 'DECODE');

		$cert_file = $is_wish == 1 ? 'store_wechat_refund_all.pem' : $_W['uniacid'] . '_wechat_refund_all.pem';
		file_put_contents(ATTACHMENT_ROOT . $cert_file, $cert . $key);
		return $refund_param;
	}
	public function xcxmoviesuccessNotify($order)
	{
		global $_GPC;
		global $_W;
		$config = $this->module['config'];
		load()->model('mc');
		if (!empty($_W['uniacid'])) {
			$_W["account"] = uni_fetch($_W['uniacid']);
		}
		if (empty($_W["account"])) {
			exit("initial error hash or id");
		}
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
		$dd = array();
		$dd['touser'] = $order['openid'];
		$dd['template_id'] = $config["movie_tpl_1"];
		$dd['page'] = 'pages/user/movieorder';
		$dd['data'] = array(
			'thing1'=>array(
				"value"=>$order['filmName']
			),
			'thing2'=>array(
				"value"=>$order['cinemaName']
			),
			'date4'=>array(
				"value"=>$order['showTime']
			),
			'thing5'=>array(
				"value"=>$order['hallName']
			),
			'thing6'=>array(
				"value"=>$order['realSeat']
			)
		);
		load()->func('communication');
		$res = ihttp_request($url,json_encode($dd));
		$res1 = json_decode($res["content"],true);
		if($res1['errcode'] == 0){
			return true;
		}else{
			$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $id, "api_appKey" => '电影票出票通知',"apiname" => 'xcxmoviesuccessNotify',"create_time" => $_W["timestamp"], "log" => json_encode($res1)));
			return false;
		}
	}
	public function get_sign($params, $secret)
	{
		$string = "";
		ksort($params);
		foreach ($params as $k => $v) {
			if ($k != "sign" && !is_array($v)) {
				$string .= $k . "=" . $v . "&";
			}
		}
		$string = trim($string, "&");
		$string = $string . "&appSecret=".$secret;
		$string = md5($string);
		return $string;
	}
	public function xcxmovierefundNotify($order)
	{
		global $_GPC;
		global $_W;
		$config = $this->module['config'];
		load()->model('mc');
		if (!empty($_W['uniacid'])) {
			$_W["account"] = uni_fetch($_W['uniacid']);
		}
		if (empty($_W["account"])) {
			exit("initial error hash or id");
		}
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
		$dd = array();
		$dd['touser'] = $order['openid'];
		$dd['template_id'] = $config["movie_tpl_2"];
		$dd['page'] = 'pages/user/movieorder';
		$dd['data'] = array(
			'thing1'=>array(
				"value"=>'出票失败，已退款，请选择其他场次购票。'
			),
			'thing2'=>array(
				"value"=>$order['cinemaName']
			),
			'thing3'=>array(
				"value"=>$order['filmName']
			)
		);
		load()->func('communication');
		$res = ihttp_request($url,json_encode($dd));
		$res1 = json_decode($res["content"],true);
		if($res1['errcode'] == 0){
			return true;
		}else{
			$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $id, "api_appKey" => '电影票退款通知',"apiname" => 'xcxmovierefundNotify',"create_time" => $_W["timestamp"], "log" => json_encode($res1)));
			return false;
		}
	}
	public function fee($uid,$id)
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => $this->modulename , "uniacid" =>$_W['uniacid']));
		$settings = unserialize($settings['settings']);
		if($settings['system_type'] == 1){
			$u = pdo_get($this->modulename."_user_u", array("uid" => $uid, "system_id" => $settings['system_id']));
		}else{
			$u = pdo_get($this->modulename."_user", array("uid" => $uid , "uniacid" => $id));
		}
		if($u){
			//如果是普通会员
			$t = 0;$h = 0;$h1 = 0;$h2 = 0;$h3 = 0;$h4 = 0;
			if($u['is_tuan'] == 0){
				$c_1_uid = $u['uid'];
				$c_1_openid = $u['openid'];
				$c_1_s1 = $u['s_1_num'];
				$c_1_fee = $settings['c_1'];
				if($u['invite_uid'] != '0'){
					$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
					$i1 = 1;
					foreach($fa_uid_array as $v){
						if($settings['system_type'] == 1){
							$u1 = pdo_get($this->modulename."_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
						}else{
							$u1 = pdo_get($this->modulename."_user", array("uid" => $v , "uniacid" => $id));
						}
						if($u1['is_tuan'] == 0 && $i1 == 1){//如果是普通会员直推
							$c_2_uid = $u1['uid'];
							$c_2_openid = $u1['openid'];
							$c_2_s1 = $u1['s_1_num'];
							$c_2_fee = $settings['c_2'];
							$i2 = 1;
							foreach($fa_uid_array as $v2){
								if($settings['system_type'] == 1){
									$u2 = pdo_get($this->modulename."_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
								}else{
									$u2 = pdo_get($this->modulename."_user", array("uid" => $v2 , "uniacid" => $id));
								}
								if($u2['is_tuan'] == 0 && $i2 == 2){//如果是普通会员间推
									$c_3_uid = $u2['uid'];
									$c_3_openid = $u2['openid'];
									$c_3_s1 = $u2['s_1_num'];
									$c_3_fee = $settings['c_3'];
									$i3 = 1;
									foreach($fa_uid_array as $v3){
										if($settings['system_type'] == 1 && $i3 > 2){
											$u3 = pdo_get($this->modulename."_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
										}elseif($i3 > 2){
											$u3 = pdo_get($this->modulename."_user", array("uid" => $v3 , "uniacid" => $id));
										}
										if($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0 && $i3 > 2){//团长非直属
											$c_t_uid = $u3['uid'];
											$c_t_openid = $u3['openid'];
											$c_t_s1 = $u3['s_1_num'];
											$c_t_fee = $settings['c_t'];
											$t = 1;
											continue;
										}elseif($u3['is_tuan'] == 1 && $t == 1 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
											$c_2t_uid = $u3['uid'];
											$c_2t_openid = $u3['openid'];
											$c_2t_s1 = $u3['s_1_num'];
											$c_2t_fee = $settings['c_2t'];
											$t = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $t == 0 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人非直属会员
											$c_t_uid = $u3['uid'];
											$c_t_openid = $u3['openid'];
											$c_t_s1 = $u3['s_1_num'];
											$c_t_fee = $settings['c_h_3'];
											$h = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $t > 0 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人直属团队
											$c_1h_uid = $u3['uid'];
											$c_1h_openid = $u3['openid'];
											$c_1h_s1 = $u3['s_1_num'];
											$c_1h_fee = $settings['c_1h'];
											$h = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $t > 0 && $h == 1 && $h1 == 0 && $i3 > 2){//合伙人间属团队
											$c_2h_uid = $u3['uid'];
											$c_2h_openid = $u3['openid'];
											$c_2h_s1 = $u3['s_1_num'];
											$c_2h_fee = $settings['c_2h'];
											$h = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
											$c_3h_uid = $u3['uid'];
											$c_3h_openid = $u3['openid'];
											$c_3h_s1 = $u3['s_1_num'];
											$c_3h_fee = $settings['c_3h'];
											$h1 = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
											$c_4h_uid = $u3['uid'];
											$c_4h_openid = $u3['openid'];
											$c_4h_s1 = $u3['s_1_num'];
											$c_4h_fee = $settings['c_4h'];
											$h1 = 2;
											break;
										}
										$i3++;
									}
									break;
								}elseif($u2['is_tuan'] == 1 && $i2 == 2){//如果是团长间推
									$c_t_uid = $u2['uid'];
									$c_t_openid = $u2['openid'];
									$c_t_s1 = $u2['s_1_num'];
									$c_t_fee = $settings['c_t'];
									$i3 = 1;
									foreach($fa_uid_array as $v3){
										if($settings['system_type'] == 1 && $i3 > 2){
											$u3 = pdo_get($this->modulename."_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
										}elseif($i3 > 2){
											$u3 = pdo_get($this->modulename."_user", array("uid" => $v3 , "uniacid" => $id));
										}
										if($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
											$c_2t_uid = $u3['uid'];
											$c_2t_openid = $u3['openid'];
											$c_2t_s1 = $u3['s_1_num'];
											$c_2t_fee = $settings['c_2t'];
											$t = 2;
											continue;
										}elseif($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
											$c_1h_uid = $u3['uid'];
											$c_1h_openid = $u3['openid'];
											$c_1h_s1 = $u3['s_1_num'];
											$c_1h_fee = $settings['c_1h'];
											$h = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人直属团队
											$c_1h_uid = $u3['uid'];
											$c_1h_openid = $u3['openid'];
											$c_1h_s1 = $u3['s_1_num'];
											$c_1h_fee = $settings['c_1h'];
											$h = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i3 > 2){//合伙人间属团队
											$c_2h_uid = $u3['uid'];
											$c_2h_openid = $u3['openid'];
											$c_2h_s1 = $u3['s_1_num'];
											$c_2h_fee = $settings['c_2h'];
											$h = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
											$c_3h_uid = $u3['uid'];
											$c_3h_openid = $u3['openid'];
											$c_3h_s1 = $u3['s_1_num'];
											$c_3h_fee = $settings['c_3h'];
											$h1 = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
											$c_4h_uid = $u3['uid'];
											$c_4h_openid = $u3['openid'];
											$c_4h_s1 = $u3['s_1_num'];
											$c_4h_fee = $settings['c_4h'];
											$h1 = 2;
											break;
										}
										$i3++;
									}
									break;
								}elseif($u2['is_tuan'] == 2 && $i2 == 2){//如果是合伙人间推
									$c_t_uid = $u2['uid'];
									$c_t_openid = $u2['openid'];
									$c_t_s1 = $u2['s_1_num'];
									$c_t_fee = $settings['c_h_3'];
									$i3 = 1;
									foreach($fa_uid_array as $v3){
										if($settings['system_type'] == 1 && $i3 > 2){
											$u3 = pdo_get($this->modulename."_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
										}elseif($i3 > 2){
											$u3 = pdo_get($this->modulename."_user", array("uid" => $v3 , "uniacid" => $id));
										}
										if($u3['is_tuan'] == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
											$c_3h_uid = $u3['uid'];
											$c_3h_openid = $u3['openid'];
											$c_3h_s1 = $u3['s_1_num'];
											$c_3h_fee = $settings['c_3h'];
											$h1 = 1;
											continue;;
										}
										if($u3['is_tuan'] == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
											$c_4h_uid = $u3['uid'];
											$c_4h_openid = $u3['openid'];
											$c_4h_s1 = $u3['s_1_num'];
											$c_4h_fee = $settings['c_4h'];
											$h1 = 2;
											break;
										}
										$i3++;
									}
									break;
								}
								$i2++;
							}
							break;
						}elseif($u1['is_tuan'] == 1 && $i1 == 1){//如果是团长直推
							$c_2_uid = $u1['uid'];
							$c_2_openid = $u1['openid'];
							$c_2_s1 = $u1['s_1_num'];
							$c_2_fee = $settings['c_t_2'];
							$i2 = 1;
							foreach($fa_uid_array as $v2){
								if($settings['system_type'] == 1){
									$u2 = pdo_get($this->modulename."_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
								}else{
									$u2 = pdo_get($this->modulename."_user", array("uid" => $v2 , "uniacid" => $id));
								}
								if($u2['is_tuan'] == 1 && $t == 0  && $h == 0 && $h1 == 0 && $i2 > 1){//如果是团长间推
									$c_2t_uid = $u2['uid'];
									$c_2t_openid = $u2['openid'];
									$c_2t_s1 = $u2['s_1_num'];
									$c_2t_fee = $settings['c_2t'];
									$t = 2;
									continue;
								}elseif($u2['is_tuan'] == 2 && $t == 0  && $h == 0 && $h1 == 0 && $i2 > 1){//如果是团长间推
									$c_1h_uid = $u2['uid'];
									$c_1h_openid = $u2['openid'];
									$c_1h_s1 = $u2['s_1_num'];
									$c_1h_fee = $settings['c_1h'];
									$h = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i2 > 1){//合伙人直属团队
									$c_1h_uid = $u2['uid'];
									$c_1h_openid = $u2['openid'];
									$c_1h_s1 = $u2['s_1_num'];
									$c_1h_fee = $settings['c_1h'];
									$h = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i2 > 1){//合伙人间属团队
									$c_2h_uid = $u2['uid'];
									$c_2h_openid = $u2['openid'];
									$c_2h_s1 = $u2['s_1_num'];
									$c_2h_fee = $settings['c_2h'];
									$h = 2;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i2 > 1){//合伙人直属合伙人
									$c_3h_uid = $u2['uid'];
									$c_3h_openid = $u2['openid'];
									$c_3h_s1 = $u2['s_1_num'];
									$c_3h_fee = $settings['c_3h'];
									$h1 = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i2 > 1){//合伙人间属合伙人
									$c_4h_uid = $u2['uid'];
									$c_4h_openid = $u2['openid'];
									$c_4h_s1 = $u2['s_1_num'];
									$c_4h_fee = $settings['c_4h'];
									$h1 = 2;
									break;
								}
								$i2++;
							}
							break;
						}elseif($u1['is_tuan'] == 2 && $i1 == 1){//如果是合伙人直推
							$c_2_uid = $u1['uid'];
							$c_2_openid = $u1['openid'];
							$c_2_s1 = $u1['s_1_num'];
							$c_2_fee = $settings['c_h_2'];
							$i2 = 1;
							foreach($fa_uid_array as $v2){
								if($settings['system_type'] == 1 && $i2 > 1){
									$u2 = pdo_get($this->modulename."_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
								}elseif($i2 > 1){
									$u2 = pdo_get($this->modulename."_user", array("uid" => $v2 , "uniacid" => $id));
								}
								if($u2['is_tuan'] == 2 && $h1 == 0 && $i2 > 1){//合伙人直属合伙人
									$c_3h_uid = $u2['uid'];
									$c_3h_openid = $u2['openid'];
									$c_3h_s1 = $u2['s_1_num'];
									$c_3h_fee = $settings['c_3h'];
									$h1 = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h1 == 1 && $i2 > 1){//合伙人间属合伙人
									$c_4h_uid = $u2['uid'];
									$c_4h_openid = $u2['openid'];
									$c_4h_s1 = $u2['s_1_num'];
									$c_4h_fee = $settings['c_4h'];
									$h1 = 2;
									break;
								}
								$i2++;
							}
							break;
						}
						$i1++;
					}
				}
			}elseif($u['is_tuan'] == 1){//如果是团长
				$c_1_uid = $u['uid'];
				$c_1_openid = $u['openid'];
				$c_1_s1 = $u['s_1_num'];
				$c_1_fee = $settings['c_t_1'];
				if($u['invite_uid'] != '0'){
					$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
					$i1 = 1;
					foreach($fa_uid_array as $v){
						if($settings['system_type'] == 1){
							$u1 = pdo_get($this->modulename."_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
						}else{
							$u1 = pdo_get($this->modulename."_user", array("uid" => $v , "uniacid" => $id));
						}
						if($u1['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0 && $i1 == 1){//如果是团长直推
							$c_2t_uid = $u1['uid'];
							$c_2t_openid = $u1['openid'];
							$c_2t_s1 = $u1['s_1_num'];
							$c_2t_fee = $settings['c_2t'];
							$t = 2;
							continue;
						}elseif($u1['is_tuan'] == 2 && $t == 0 && $h == 0 && $h1 == 0 && $i1 == 1){//如果是合伙人直推
							$c_1h_uid = $u1['uid'];
							$c_1h_openid = $u1['openid'];
							$c_1h_s1 = $u1['s_1_num'];
							$c_1h_fee = $settings['c_1h'];
							$h = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i1 > 1){//合伙人直属团队
							$c_1h_uid = $u1['uid'];
							$c_1h_openid = $u1['openid'];
							$c_1h_s1 = $u1['s_1_num'];
							$c_1h_fee = $settings['c_1h'];
							$h = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i1 > 1){//合伙人间属团队
							$c_2h_uid = $u1['uid'];
							$c_2h_openid = $u1['openid'];
							$c_2h_s1 = $u1['s_1_num'];
							$c_2h_fee = $settings['c_2h'];
							$h = 2;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i1 > 1){//合伙人直属合伙人
							$c_3h_uid = $u1['uid'];
							$c_3h_openid = $u1['openid'];
							$c_3h_s1 = $u1['s_1_num'];
							$c_3h_fee = $settings['c_3h'];
							$h1 = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i1 > 1){//合伙人间属合伙人
							$c_4h_uid = $u1['uid'];
							$c_4h_openid = $u1['openid'];
							$c_4h_s1 = $u1['s_1_num'];
							$c_4h_fee = $settings['c_4h'];
							$h1 = 2;
							break;
						}
						$i1++;
					}
				}
			}elseif($u['is_tuan'] == 2){//如果是合伙人
				$c_1_uid = $u['uid'];
				$c_1_openid = $u['openid'];
				$c_1_s1 = $u['s_1_num'];
				$c_1_fee = $settings['c_h_1'];
				if($u['invite_uid'] != '0'){
					$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
					$i1 = 1;
					foreach($fa_uid_array as $v){
						if($settings['system_type'] == 1){
							$u1 = pdo_get($this->modulename."_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
						}else{
							$u1 = pdo_get($this->modulename."_user", array("uid" => $v , "uniacid" => $id));
						}
						if($u1['is_tuan'] == 2 && $h1 == 0 && $i1 > 0){//合伙人直属合伙人
							$c_3h_uid = $u1['uid'];
							$c_3h_openid = $u1['openid'];
							$c_3h_s1 = $u1['s_1_num'];
							$c_3h_fee = $settings['c_3h'];
							$h1 = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h1 == 1 && $i1 > 0){//合伙人间属合伙人
							$c_4h_uid = $u1['uid'];
							$c_4h_openid = $u1['openid'];
							$c_4h_s1 = $u1['s_1_num'];
							$c_4h_fee = $settings['c_4h'];
							$h1 = 2;
							break;
						}
						$i1++;
					}
				}
			}
		}
		$c1_uid = $c_1_uid;
		if(!$c_1_uid || !$c_1_fee){
			$c_1_uid = 0;
			$c_1_openid = '';
			$c_1_s1 = 0;
			$c_1_fee = 0;
		}
		if(!$c_2_uid || !$c_2_fee){
			$c_2_uid = 0;
			$c_2_openid = '';
			$c_2_s1 = 0;
			$c_2_fee = 0;
		}
		if(!$c_3_uid || !$c_3_fee){
			$c_3_uid = 0;
			$c_3_openid = '';
			$c_3_s1 = 0;
			$c_3_fee = 0;
		}
		if(!$c_t_uid || !$c_t_fee){
			$c_t_uid = 0;
			$c_t_openid = '';
			$c_t_s1 = 0;
			$c_t_fee = 0;
		}
		if(!$c_2t_uid || !$c_2t_fee){
			$c_2t_uid = 0;
			$c_2t_openid = '';
			$c_2t_s1 = 0;
			$c_2t_fee = 0;
		}
		if(!$c_1h_uid || !$c_1h_fee){
			$c_1h_uid = 0;
			$c_1h_openid = '';
			$c_1h_s1 = 0;
			$c_1h_fee = 0;
		}
		if(!$c_2h_uid || !$c_2h_fee){
			$c_2h_uid = 0;
			$c_2h_openid = '';
			$c_2h_s1 = 0;
			$c_2h_fee = 0;
		}
		if(!$c_3h_uid || !$c_3h_fee){
			$c_3h_uid = 0;
			$c_3h_openid = '';
			$c_3h_s1 = 0;
			$c_3h_fee = 0;
		}
		if(!$c_4h_uid || !$c_4h_fee){
			$c_4h_uid = 0;
			$c_4h_openid = '';
			$c_4h_s1 = 0;
			$c_4h_fee = 0;
		}
		$fee = array(
			'c1_uid' => $c1_uid,
			'c_1_uid' => $c_1_uid,
			'c_1_openid' => $c_1_openid,
			'c_1_s1' => $c_1_s1,
			'c_1_fee' => $c_1_fee,
			'c_2_uid' => $c_2_uid,
			'c_2_openid' => $c_2_openid,
			'c_2_s1' => $c_2_s1,
			'c_2_fee' => $c_2_fee,
			'c_3_uid' => $c_3_uid,
			'c_3_openid' => $c_3_openid,
			'c_3_s1' => $c_3_s1,
			'c_3_fee' => $c_3_fee,
			'c_t_uid' => $c_t_uid,
			'c_t_openid' => $c_t_openid,
			'c_t_s1' => $c_t_s1,
			'c_t_fee' => $c_t_fee,
			'c_2t_uid' => $c_2t_uid,
			'c_2t_openid' => $c_2t_openid,
			'c_2t_s1' => $c_2t_s1,
			'c_2t_fee' => $c_2t_fee,
			'c_1h_uid' => $c_1h_uid,
			'c_1h_openid' => $c_1h_openid,
			'c_1h_s1' => $c_1h_s1,
			'c_1h_fee' => $c_1h_fee,
			'c_2h_uid' => $c_2h_uid,
			'c_2h_openid' => $c_2h_openid,
			'c_2h_s1' => $c_2h_s1,
			'c_2h_fee' => $c_2h_fee,
			'c_3h_uid' => $c_3h_uid,
			'c_3h_openid' => $c_3h_openid,
			'c_3h_s1' => $c_3h_s1,
			'c_3h_fee' => $c_3h_fee,
			'c_4h_uid' => $c_4h_uid,
			'c_4h_openid' => $c_4h_openid,
			'c_4h_s1' => $c_4h_s1,
			'c_4h_fee' => $c_4h_fee
		);
		return $fee;
		
	}

}

function geturl($url,$data){
		$postdata = http_build_query($data);
		//if($url == 'https://yp-api.taototo.cn/yp-api/movie/schedule/schedules-list-tpp'){echo $postdata;}
		
		$options = array(
				'http' => array(
				'method' => 'POST',
				'header' => 'Content-type:application/x-www-form-urlencoded',
				'content' => $postdata,
				'timeout' => 1 * 60 // 超时时间（单位:s）
			  )
			);
		$cxContext = stream_context_create($options);
		$result = file_get_contents($url, false, $cxContext);
		$result = json_decode($result,true);
		return $result;
	}

function get_sign($params, $secret)
	{
		$string = "";
		ksort($params);
		foreach ($params as $k => $v) {
			if ($k != "sign" && !is_array($v)) {
				$string .= $k . "=" . $v . "&";
			}
		}
		$string = trim($string, "&");
		$string = $string . "&appSecret=".$secret;
		$string = md5($string);
		return $string;
	}

function pagination1($total, $pageIndex, $pageSize = 15, $url = '', $context = array('before' => 5, 'after' => 4, 'ajaxcallback' => '', 'callbackfuncname' => '')) {
	global $_W;
	$pdata = array(
		'tcount' => 0,
		'tpage' => 0,
		'cindex' => 0,
		'findex' => 0,
		'pindex' => 0,
		'nindex' => 0,
		'lindex' => 0,
		'options' => '',
	);
	if (empty($context['before'])) {
		$context['before'] = 5;
	}
	if (empty($context['after'])) {
		$context['after'] = 4;
	}

	if ($context['ajaxcallback']) {
		$context['isajax'] = true;
	}

	if ($context['callbackfuncname']) {
		$callbackfunc = $context['callbackfuncname'];
	}

	$pdata['tcount'] = $total;
	$pdata['tpage'] = (empty($pageSize) || $pageSize < 0) ? 1 : ceil($total / $pageSize);
	if ($pdata['tpage'] <= 1) {
		return '';
	}
	$cindex = $pageIndex;
	$cindex = min($cindex, $pdata['tpage']);
	$cindex = max($cindex, 1);
	$pdata['cindex'] = $cindex;
	$pdata['findex'] = 1;
	$pdata['pindex'] = $cindex > 1 ? $cindex - 1 : 1;
	$pdata['nindex'] = $cindex < $pdata['tpage'] ? $cindex + 1 : $pdata['tpage'];
	$pdata['lindex'] = $pdata['tpage'];

	if ($context['isajax']) {
		if (empty($url)) {
			$url = $_W['script_name'] . '?' . http_build_query($_GET);
		}
		$pdata['faa'] = 'href="javascript:;" page1="' . $pdata['findex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['findex'] . '\', this);"' : '');
		$pdata['paa'] = 'href="javascript:;" page1="' . $pdata['pindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['pindex'] . '\', this);"' : '');
		$pdata['naa'] = 'href="javascript:;" page1="' . $pdata['nindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['nindex'] . '\', this);"' : '');
		$pdata['laa'] = 'href="javascript:;" page1="' . $pdata['lindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['lindex'] . '\', this);"' : '');
	} else {
		if ($url) {
			$pdata['faa'] = 'href="?' . str_replace('*', $pdata['findex'], $url) . '"';
			$pdata['paa'] = 'href="?' . str_replace('*', $pdata['pindex'], $url) . '"';
			$pdata['naa'] = 'href="?' . str_replace('*', $pdata['nindex'], $url) . '"';
			$pdata['laa'] = 'href="?' . str_replace('*', $pdata['lindex'], $url) . '"';
		} else {
			$_GET['page1'] = $pdata['findex'];
			$pdata['faa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
			$_GET['page1'] = $pdata['pindex'];
			$pdata['paa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
			$_GET['page1'] = $pdata['nindex'];
			$pdata['naa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
			$_GET['page1'] = $pdata['lindex'];
			$pdata['laa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
		}
	}

	$html = '<div><ul class="pagination pagination-centered">';
	$html .= "<li><a {$pdata['faa']} class=\"pager-nav\">首页</a></li>";
	empty($callbackfunc) && $html .= "<li><a {$pdata['paa']} class=\"pager-nav\">&laquo;上一页</a></li>";

		if (!$context['before'] && 0 != $context['before']) {
		$context['before'] = 5;
	}
	if (!$context['after'] && 0 != $context['after']) {
		$context['after'] = 4;
	}

	if (0 != $context['after'] && 0 != $context['before']) {
		$range = array();
		$range['start'] = max(1, $pdata['cindex'] - $context['before']);
		$range['end'] = min($pdata['tpage'], $pdata['cindex'] + $context['after']);
		if ($range['end'] - $range['start'] < $context['before'] + $context['after']) {
			$range['end'] = min($pdata['tpage'], $range['start'] + $context['before'] + $context['after']);
			$range['start'] = max(1, $range['end'] - $context['before'] - $context['after']);
		}
		for ($i = $range['start']; $i <= $range['end']; ++$i) {
			if ($context['isajax']) {
				$aa = 'href="javascript:;" page1="' . $i . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $i . '\', this);"' : '');
			} else {
				if ($url) {
					$aa = 'href="?' . str_replace('*', $i, $url) . '"';
				} else {
					$_GET['page1'] = $i;
					$aa = 'href="?' . http_build_query($_GET) . '"';
				}
			}
			if (!empty($context['isajax'])) {
				$html .= ($i == $pdata['cindex'] ? '<li class="active">' : '<li>') . "<a {$aa}>" . $i . '</a></li>';
			} else {
				$html .= ($i == $pdata['cindex'] ? '<li class="active"><a href="javascript:;">' . $i . '</a></li>' : "<li><a {$aa}>" . $i . '</a></li>');
			}
		}
	}

	if ($pdata['cindex'] < $pdata['tpage']) {
		empty($callbackfunc) && $html .= "<li><a {$pdata['naa']} class=\"pager-nav\">下一页&raquo;</a></li>";
		$html .= "<li><a {$pdata['laa']} class=\"pager-nav\">尾页</a></li>";
	}
	$html .= '</ul></div>';

	return $html;
}


function pagination2($total, $pageIndex, $pageSize = 15, $url = '', $context = array('before' => 5, 'after' => 4, 'ajaxcallback' => '', 'callbackfuncname' => '')) {
	global $_W;
	$pdata = array(
		'tcount' => 0,
		'tpage' => 0,
		'cindex' => 0,
		'findex' => 0,
		'pindex' => 0,
		'nindex' => 0,
		'lindex' => 0,
		'options' => '',
	);
	if (empty($context['before'])) {
		$context['before'] = 5;
	}
	if (empty($context['after'])) {
		$context['after'] = 4;
	}

	if ($context['ajaxcallback']) {
		$context['isajax'] = true;
	}

	if ($context['callbackfuncname']) {
		$callbackfunc = $context['callbackfuncname'];
	}

	$pdata['tcount'] = $total;
	$pdata['tpage'] = (empty($pageSize) || $pageSize < 0) ? 1 : ceil($total / $pageSize);
	if ($pdata['tpage'] <= 1) {
		return '';
	}
	$cindex = $pageIndex;
	$cindex = min($cindex, $pdata['tpage']);
	$cindex = max($cindex, 1);
	$pdata['cindex'] = $cindex;
	$pdata['findex'] = 1;
	$pdata['pindex'] = $cindex > 1 ? $cindex - 1 : 1;
	$pdata['nindex'] = $cindex < $pdata['tpage'] ? $cindex + 1 : $pdata['tpage'];
	$pdata['lindex'] = $pdata['tpage'];

	if ($context['isajax']) {
		if (empty($url)) {
			$url = $_W['script_name'] . '?' . http_build_query($_GET);
		}
		$pdata['faa'] = 'href="javascript:;" page2="' . $pdata['findex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['findex'] . '\', this);"' : '');
		$pdata['paa'] = 'href="javascript:;" page2="' . $pdata['pindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['pindex'] . '\', this);"' : '');
		$pdata['naa'] = 'href="javascript:;" page2="' . $pdata['nindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['nindex'] . '\', this);"' : '');
		$pdata['laa'] = 'href="javascript:;" page2="' . $pdata['lindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['lindex'] . '\', this);"' : '');
	} else {
		if ($url) {
			$pdata['faa'] = 'href="?' . str_replace('*', $pdata['findex'], $url) . '"';
			$pdata['paa'] = 'href="?' . str_replace('*', $pdata['pindex'], $url) . '"';
			$pdata['naa'] = 'href="?' . str_replace('*', $pdata['nindex'], $url) . '"';
			$pdata['laa'] = 'href="?' . str_replace('*', $pdata['lindex'], $url) . '"';
		} else {
			$_GET['page2'] = $pdata['findex'];
			$pdata['faa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
			$_GET['page2'] = $pdata['pindex'];
			$pdata['paa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
			$_GET['page2'] = $pdata['nindex'];
			$pdata['naa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
			$_GET['page2'] = $pdata['lindex'];
			$pdata['laa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
		}
	}

	$html = '<div><ul class="pagination pagination-centered">';
	$html .= "<li><a {$pdata['faa']} class=\"pager-nav\">首页</a></li>";
	empty($callbackfunc) && $html .= "<li><a {$pdata['paa']} class=\"pager-nav\">&laquo;上一页</a></li>";

		if (!$context['before'] && 0 != $context['before']) {
		$context['before'] = 5;
	}
	if (!$context['after'] && 0 != $context['after']) {
		$context['after'] = 4;
	}

	if (0 != $context['after'] && 0 != $context['before']) {
		$range = array();
		$range['start'] = max(1, $pdata['cindex'] - $context['before']);
		$range['end'] = min($pdata['tpage'], $pdata['cindex'] + $context['after']);
		if ($range['end'] - $range['start'] < $context['before'] + $context['after']) {
			$range['end'] = min($pdata['tpage'], $range['start'] + $context['before'] + $context['after']);
			$range['start'] = max(1, $range['end'] - $context['before'] - $context['after']);
		}
		for ($i = $range['start']; $i <= $range['end']; ++$i) {
			if ($context['isajax']) {
				$aa = 'href="javascript:;" page2="' . $i . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $i . '\', this);"' : '');
			} else {
				if ($url) {
					$aa = 'href="?' . str_replace('*', $i, $url) . '"';
				} else {
					$_GET['page2'] = $i;
					$aa = 'href="?' . http_build_query($_GET) . '"';
				}
			}
			if (!empty($context['isajax'])) {
				$html .= ($i == $pdata['cindex'] ? '<li class="active">' : '<li>') . "<a {$aa}>" . $i . '</a></li>';
			} else {
				$html .= ($i == $pdata['cindex'] ? '<li class="active"><a href="javascript:;">' . $i . '</a></li>' : "<li><a {$aa}>" . $i . '</a></li>');
			}
		}
	}

	if ($pdata['cindex'] < $pdata['tpage']) {
		empty($callbackfunc) && $html .= "<li><a {$pdata['naa']} class=\"pager-nav\">下一页&raquo;</a></li>";
		$html .= "<li><a {$pdata['laa']} class=\"pager-nav\">尾页</a></li>";
	}
	$html .= '</ul></div>';

	return $html;
}

